package com.plugin.planilha;

import java.util.HashMap;
import java.util.Map;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseMapSheet implements ISheet{
	
	private WritableWorkbook workbook;
	private HashMap<String, Map<String, Integer[]>> map;
	private static WritableSheet metricaMapWS;

	
	public AnaliseMapSheet(WritableWorkbook w, HashMap<String, Map<String, Integer[]>> map) {
		workbook = w;
		this.map = map;
		metricaMapWS = workbook.createSheet("Values e keySet em Map", 1);
	}

	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {
		
		Label label = new Label(0, 0, "Variavel", cf);
		metricaMapWS.addCell(label);
		
		label = new Label(1, 0, "Numero de values()", cf);
		metricaMapWS.addCell(label);
		
		label = new Label(2, 0, "Numero de keySet()", cf);
		metricaMapWS.addCell(label);
		
		label = new Label(3, 0, "Metodos lineares", cf);
		metricaMapWS.addCell(label);
		
		label = new Label(5, 0, "Total values()", cf);
		metricaMapWS.addCell(label);
		
		label = new Label(6, 0, "Total keySet()", cf);
		metricaMapWS.addCell(label);

		label = new Label(7, 0, "Total variaveis utilizam metodos lineares", cf);
		metricaMapWS.addCell(label);

		label = new Label(8, 0, "Total variaveis nao utilizam metodos lineares", cf);
		metricaMapWS.addCell(label);

		label = new Label(9, 0, "Taxa de variaveis que utilizam metodos lineares", cf);
		metricaMapWS.addCell(label);
		
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {
		Label label;
		jxl.write.Number n;
		int indice = 1;
		double total_values=0, total_keyset=0;
		double total_variaveis_metodos_lineares=0, total_variaveis_no_metodos_lineares=0;
		
		for (String ch : map.keySet()) {
			if (map.get(ch).size() > 0) 
				for (String variavel : map.get(ch).keySet()) {
					int values = map.get(ch).get(variavel)[0];
					int keyset = map.get(ch).get(variavel)[1];
					total_values += values;
					total_keyset += keyset;
					boolean metodos_lineares = (values > 0 || keyset > 0) ? true: false;
					
					label = new Label(0, indice, variavel);
					metricaMapWS.addCell(label);

					n = new jxl.write.Number(1, indice,
							(int)values);
					metricaMapWS.addCell(n);

					n = new jxl.write.Number(2, indice,
							(int) keyset);
					metricaMapWS.addCell(n);

					label = new Label(3, indice, Boolean.toString(metodos_lineares));
					metricaMapWS.addCell(label);
					
					if (metodos_lineares)
						total_variaveis_metodos_lineares++;
					else
						total_variaveis_no_metodos_lineares++;
					indice++;
				}
		}
		double round_total=0;
		if((total_variaveis_metodos_lineares + total_variaveis_no_metodos_lineares)!=0){
		double total = (100 * total_variaveis_metodos_lineares)
				/ (total_variaveis_metodos_lineares + total_variaveis_no_metodos_lineares);
		round_total = (double) Math.round(total * 1000) / 1000;}
		
		n = new jxl.write.Number(5, 1, (int) total_values);
		metricaMapWS.addCell(n);

		n = new jxl.write.Number(6, 1, (int) total_keyset);
		metricaMapWS.addCell(n);

		n = new jxl.write.Number(7, 1, (int) total_variaveis_metodos_lineares);
		metricaMapWS.addCell(n);

		n = new jxl.write.Number(8, 1, (int) total_variaveis_no_metodos_lineares);
		metricaMapWS.addCell(n);

		n = new jxl.write.Number(9, 1, round_total);
		metricaMapWS.addCell(n);
		
	}
	
	
	

}
